package com.ht.WeNas.rest_controller;

import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.ht.WeNas.config.data.CacheNameEnum;
import com.ht.WeNas.data.vo.Res;
import com.ht.WeNas.utils.CacheUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.SystemInfo;
import oshi.hardware.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "系统信息控制器")
@RestController
@RequestMapping("/api/sys")
public class SystemInfoController {
    SystemInfo systemInfo = new SystemInfo();
    HardwareAbstractionLayer hardware = systemInfo.getHardware();

    private final CacheUtil cacheUtil;

    public SystemInfoController(CacheUtil cacheUtil) {
        this.cacheUtil = cacheUtil;
    }

    @Operation(summary = "内存相关")
    @GetMapping("/memory")
    public Res<GlobalMemory> getMemoryUsage() {
        GlobalMemory memory = cacheUtil.cacheRead(
                CacheNameEnum.SysInfoMemory,
                "key",
                (key) -> OshiUtil.getMemory()
        );
        return Res.ok(memory);
    }
    
    @Operation(summary = "硬盘相关")
    @GetMapping("/disk")
    public Res<List<HWDiskStore>> getDisk() {
        List<HWDiskStore> diskStores = cacheUtil.cacheRead(
                CacheNameEnum.SysInfoDisk,
                "key",
                (key) -> OshiUtil.getDiskStores()
        );
        return Res.ok(diskStores);
    }

    @Operation(summary = "CPU相关")
    @GetMapping("/cpu")
    public Res<CpuInfo> getCpuInfo() {
        CpuInfo cpuInfo = cacheUtil.cacheRead(
                CacheNameEnum.SysInfoCpu,
                "key",
                (key) -> OshiUtil.getCpuInfo(500)
        );
        return Res.ok(cpuInfo);
    }

    @Operation(summary = "处理器相关")
    @GetMapping("/processor")
    public Res<Map<String, Object>> getProcessor() {
        CentralProcessor processor = cacheUtil.cacheRead(
                CacheNameEnum.SysInfoProcessor,
                "key",
                (key) -> OshiUtil.getProcessor()
        );
        Map<String, Object> res = new HashMap<>();
        res.put("logicalProcessors", processor.getLogicalProcessors());
        res.put("physicalProcessors", processor.getPhysicalProcessors());
        res.put("caches", processor.getProcessorCaches());
        res.put("featureFlags", processor.getFeatureFlags());
        res.put("identifier", processor.getProcessorIdentifier());
        res.put("interrupts", String.valueOf(processor.getInterrupts()));
        return Res.ok(res);
    }

    @Operation(summary = "系统相关")
    @GetMapping("/system")
    public Res<ComputerSystem> getSystem() {
        ComputerSystem system = OshiUtil.getSystem();
        return Res.ok(system);
    }

    @Operation(summary = "显卡相关")
    @GetMapping("/gpu")
    public Res<List<GraphicsCard>> getGpus() {
        List<GraphicsCard> graphicsCards = hardware.getGraphicsCards();
        return Res.ok(graphicsCards);
    }

    @Operation(summary = "网卡相关")
    @GetMapping("/network")
    public Res<Object> getNetworkIF() {
        List<NetworkIF> networkIFs = OshiUtil.getNetworkIFs();
        return Res.ok(networkIFs);
    }

    @Operation(summary = "USB树")
    @GetMapping("/usb")
    public Res<List<UsbDevice>> getUsbList() {
        List<UsbDevice> usbDevices = cacheUtil.cacheRead(
                CacheNameEnum.SysInfoUsb,
                "key",
                (key) -> hardware.getUsbDevices(true)
        );
        return Res.ok(usbDevices);
    }
}
